package com.shaw.workrecord.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shaw.workrecord.entity.PageResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jiangjunhui
 * @description
 * @date 2022/9/15 10:41
 */
public class PageUtils<T, E> {

    public static <T, E> PageResponse<E> toPageResponse(Page<T> page, Class<E> clazz) {
        List<E> collect = page.getRecords().stream().map(i -> {
            return BeanUtil.copyProperties(i, clazz);
        }).collect(Collectors.toList());
        PageResponse<E> pageResponse = new PageResponse<>();
        pageResponse.setRows(CollectionUtil.isEmpty(collect) ? new ArrayList<>() : collect);
        pageResponse.setTotal(page.getTotal());
        return pageResponse;
    }
}
